package com.fiberhome.ztelauncher.core.page.holder;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fiberhome.ztelauncher.R;
import com.fiberhome.ztelauncher.core.page.MultiTypeAdapter;
import com.fiberhome.ztelauncher.core.page.model.AppType;
import com.fiberhome.ztelauncher.util.BitmapUitl;
import com.fiberhome.ztelauncher.util.TextStyleUtil;
import com.fiberhome.ztelauncher.xml.bean.LaunchDataPage;

import java.util.List;


/**
 * Created by xc on 2018/3/27.
 */

public class AppViewHolder extends BaseViewHolder<AppType> implements BaseViewHolder.OnFocusListener{

    private List<LaunchDataPage.ElementData> listElementData;
    private Context context;
    private AppType appType;
    private int position;
    private MultiTypeAdapter adapter;
    private ImageView ivIcon;

    public AppViewHolder(View itemView) {
        super(itemView);

    }

    @Override
    public void setUpView(AppType appType, int position, MultiTypeAdapter adapter) {
        context = itemView.getContext();
        this.appType = appType;
        this.position = position;
        this.adapter = adapter;
        setItemViewParams(appType);
        listElementData = appType.getDataElement().elementDatas.elementDatas;

        initView();
        adapter.setOnFocusListener(this);
    }

    private void initView(){
        ivIcon = (ImageView) getView(R.id.iv_icon);
        TextView tvName = (TextView) getView(R.id.tv_name);

        ivIcon.setScaleType(ImageView.ScaleType.FIT_XY);
        tvName.setTextColor(Color.parseColor(appType.getPage().fontColor));
        tvName.setTextSize(TypedValue.COMPLEX_UNIT_PX, appType.getPage().fontSize);
        tvName.setText(listElementData.get(0).elementName);
        tvName.getPaint().setTypeface(TextStyleUtil.getTextStyle(appType.getPage().fontStyle));
        BitmapUitl.loadBackground(context, itemView, listElementData.get(0).backGroundContentURL);
        BitmapUitl.loadBitmap(context, ivIcon, listElementData.get(0).contentURL);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LaunchDataPage.ElementData elementData = appType.getDataElement().elementDatas.elementDatas.get(0);
                adapter.clickElement(elementData.elementAction,elementData.extraData);
            }
        });
    }


    @Override
    public void onFocus(BaseViewHolder baseViewHolder, boolean isFocus, int position) {
        if(baseViewHolder instanceof AppViewHolder){
            AppViewHolder avh = (AppViewHolder) baseViewHolder;
            LaunchDataPage.ElementData elementData = adapter.getModels().get(position).getDataElement().
                    elementDatas.elementDatas.get(0);
            if (!TextUtils.isEmpty(elementData.focusContentURL)) {
                if (isFocus) {
                    BitmapUitl.loadBitmap(context, avh.ivIcon, elementData.focusContentURL);
                } else {
                    BitmapUitl.loadBitmap(context, avh.ivIcon, elementData.contentURL);
                }
            }

        }
    }
}
